package com.ljkj.cordial.chat.activity;

import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.lanjiu.lib.imclient.bus.EventConstants;
import com.lanjiu.lib.imclient.bus.MessageEvent;
import com.lanjiu.lib.imclient.dto.FriendDto;
import com.lanjiu.lib.imclient.dto.GroupDto;
import com.lanjiu.lib.imclient.dto.SettingsDto;
import com.lanjiu.lib.imclient.dto.VersionDto;
import com.lanjiu.lib.imclient.dto.notify.SysNotifyDto;
import com.lanjiu.lib.imclient.dto.user.UserDto;
import com.lanjiu.lib.imclient.ims.IMSClientHelper;
import com.lanjiu.lib.imclient.ims.IMSContacts;
import com.ljkj.cordial.chat.R;
import com.ljkj.cordial.chat.adapter.ToggleAccountAdapter;
import com.ljkj.cordial.chat.base.ChatApp;
import com.ljkj.cordial.chat.bean.Canstants;
import com.ljkj.cordial.chat.dao.DbController;
import com.ljkj.cordial.chat.dao.UserTableBean;
import com.ljkj.cordial.chat.notify.AppBadgerManager;
import com.ljkj.cordial.chat.notify.NotificationUtils;
import com.ljkj.cordial.chat.ui.AppEventActivity;
import com.ljkj.cordial.chat.ui.views.ChatMainView;
import com.ljkj.cordial.chat.util.DeleteHistoryUtil;
import com.ljkj.cordial.chat.util.ObserverThread;
import com.ljkj.cordial.chat.widget.ChatItemPop;
import com.ljkj.cordial.lengthtcpheartbeat.TcpLengthHeartbeatBroadcastReceive;
import com.nic.view.widget.CommonPopupWindow;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 账号切换
 */
public class ToggleAccountActivity extends AppEventActivity<ChatMainView> {

    @BindView(R.id.listView)
    ListView listView;
    @BindView(R.id.v_tran)
    View vTran;

    private ToggleAccountAdapter toggleAccountAdapter;
    private UserTableBean newUser;

    @Override
    public int getContentLayoutId() {
        return R.layout.activity_toggle_account;
    }

    @Override
    public void initView() {
        ButterKnife.bind(this);
        getToolbar().setTitle("账号切换");
        toggleAccountAdapter = new ToggleAccountAdapter();
        toggleAccountAdapter.setLoginOutListener((newUser) -> {
            this.newUser = newUser;
            obtainView().doLogout();
        });
        toggleAccountAdapter.setLongClickListener(new ToggleAccountAdapter.OnAccountLongClickListener() {
            @Override
            public void onAccountLongClick(View v, UserTableBean item, int position) {
                ChatItemPop pop = new ChatItemPop(mContext);
                pop.setData("删除");
                pop.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        ObserverThread.create().onNextThread0(new ObserverThread.OnActionListener<Object>() {
                            @Override
                            public void onAction(Object obj) {
                                toggleAccountAdapter.clear();
                                setData();
                            }

                            @Override
                            public Object onThreadAction() {
                                String targetId = UserTableBean.TYPE_REGISTER.equals(item.getUserType()) ? item.getUserId() : item.getTemporaryId();
                                DeleteHistoryUtil.getInstance().deleteUser(targetId);
                                return null;
                            }
                        });


                    }
                });
                pop.getPopupWindow().setOnDismissListener(() -> vTran.setVisibility(View.GONE));
                pop.showBashOfAnchor(v, new CommonPopupWindow.LayoutGravity(CommonPopupWindow.LayoutGravity.ALIGN_ABOVE | CommonPopupWindow.LayoutGravity.CENTER_HORI), 0, 14);
                vTran.setVisibility(View.VISIBLE);
                vTran.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        vTran.setVisibility(View.GONE);
                    }
                });
            }
        });
        listView.setAdapter(toggleAccountAdapter);
        setData();

    }

    private void setData() {
        ObserverThread.create().onNextThread0(new ObserverThread.OnActionListener<List<UserTableBean>>() {
            @Override
            public void onAction(List<UserTableBean> obj) {
                toggleAccountAdapter.setList(obj);
            }

            @Override
            public List<UserTableBean> onThreadAction() {
                return UserTableBean.getCreatorList();
            }
        });
    }

    @Override
    protected ChatMainView bindNettyMessageView() {
        return new ChatMainView(new ChatMainView.ChatMainCallback() {
            @Override
            public void onFriendListCallback(int code, String msg, List<FriendDto> data) {

            }

            @Override
            public void onGroupListCallback(int code, String msg, List<GroupDto> data) {

            }

            @Override
            public void onGetFriendOffMsgListCallback(int code, String msg, int page, boolean request) {

            }

            @Override
            public void onGetGroupOffMsgListCallback(int code, String msg, int page, boolean request) {

            }


            @Override
            public void onLogoutCallback(int code, String msg) {
                if (code == IMSContacts.ResponseCode.SUCCESS) {
                    doQuit();
                }

            }

            @Override
            public void onLoginCallback(int code, String msg, UserDto data) {

            }

            @Override
            public void onGetNotifyCallback(int code, String msg, List<SysNotifyDto> data) {

            }

            @Override
            public void onCheckVersionCallback(int code, String msg, VersionDto data) {

            }

            @Override
            public void onSettingsInfoCallback(int code, String msg, SettingsDto data) {

            }
        });
    }

    public void doQuit() {
        NotificationUtils.clearNotify(this);
        AppBadgerManager.clean(this);
        TcpLengthHeartbeatBroadcastReceive.loginOut();
        DeleteHistoryUtil.getInstance().clear(this, (clearCyc,isClear) -> {
            EventBus.getDefault().post(MessageEvent.createUiRefreshWidthData(EventConstants.Action.ON_DELETE_HISTORY, "-"));
//            String visitorId = ChatApp.getInstance().get().getTemporaryId();
//            if (!TextUtils.isEmpty(visitorId)) {
//                DeleteHistoryUtil.getInstance().deleteUuu(this::clear);
//            } else {
            clear();
//            }
        });
    }

    private void clear() {
        ChatApp.getInstance().remove();
        ChatApp.getInstance().put(newUser);
        ChatApp.getInstance().setExitFlag(true);
        IMSClientHelper.getInstance().close();
        DbController.getInstance().clear();
        ChatApp.getInstance().quitApp();
//        startActivity(new Intent(ChatApp.getContext(), SplashActivity.class));
        LoginActivity.jumpTo(this, Canstants.STATE_REGISTER_LOGIN);
        overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
    }
}
